﻿using UnityEngine;
using System.Collections;
using UnityEditor;
using System;
using System.IO;
using HDJ.Framework.Core;
using System.Collections.Generic;
namespace HDJ.Framework.Core.Editor
{
    public class CreateConfigClassEditorUtil
    {


        public static void CreateConfigClassFile(string configFileData, string fileName)
        {
            string data = ParseConfigClassStringData(configFileData, fileName);

            string savePath = TableDefine.SaveConfigClassPath + fileName + ".cs";
            //Debug.Log("data :" + data);
            FileUtils.CreateTextFile(savePath, data);
        }
        public static void CreateConfigClassFile(TableData tData, string fileName)
        {
            string data = ParseConfigClassStringData(tData, fileName);

            string savePath = TableDefine.SaveConfigClassPath + fileName + ".cs";
            //Debug.Log("data :" + data);
            FileUtils.CreateTextFile(savePath, data);
        }

        private static string ParseConfigClassStringData(string data, string className)
        {
            //回车换行
            string[] temp0 = data.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
            //Debug.Log("data :" + data);
            string[] needData = new string[3];
            if (temp0.Length >= 3)
            {
                for (int i = 1; i < 4; i++)
                {
                    needData[i - 1] = temp0[i];
                }
            }

            //temp0.CopyTo(needData, 0);

            string[][] temp1 = new string[needData.Length][];

            for (int i = 0; i < needData.Length; i++)
            {
                //Debug.Log(needData[i]);
                //制表符
                temp1[i] = needData[i].Split(new char[] { '\t' });
            }


            string classString = "using UnityEngine;\r\n";
            classString += "using HDJ.Framework.Core;\r\n";
            classString += "// 自动生成请勿更改\r\n";
            classString += "\t/// <summary>\r\n \t/// " + temp0[0] + "\r\n\t/// </summary>\r\n";
            classString += "public class " + className + " : " + typeof(TableBase).Name + "\r\n{ \r\n";

            for (int j = 0; j < temp1[0].Length; j++)
            {
                classString += "\t/// <summary>\r\n \t/// " + temp1[1][j] + "\r\n\t/// </summary>\r\n";
                classString += "\t public " + temp1[0][j] + " " + temp1[2][j] + ";\r\n";
            }
            classString += "}";

            //Debug.Log("classString :" + classString);
            return classString;
        }
        private static string ParseConfigClassStringData(TableData data, string className)
        {

            string classString = "using UnityEngine;\r\n";
            classString += "using HDJ.Framework.Core;\r\n";
            //classString += "using System;\r\n";
            //classString += "using System.Reflection;\r\n";
            classString += "// 自动生成请勿更改\r\n";
            classString += "\t/// <summary>\r\n \t/// " + data.description + "\r\n\t/// </summary>\r\n";
            classString += "public class " + className + " : " + typeof(TableBase).Name + "\r\n{ \r\n";

            foreach (var info in data.fieldInfos.Values)
            {
                classString += "\t/// <summary>\r\n \t/// " + info.description + "\r\n\t/// </summary>\r\n";
              //  Type type = TableUtils.TableFieldType2Type(info.fieldType, info.enumType);
                classString += "\t private " + info.fieldType.FullName + " m_" + info.name + ";\r\n";
            }
            foreach (var info in data.fieldInfos.Values)
            {
                classString += "\t/// <summary>\r\n \t/// " + info.description + "\r\n\t/// </summary>\r\n";
               // Type type = TableUtils.TableFieldType2Type(info.fieldType, info.enumType);
                classString += "\t public " + info.fieldType.FullName + " " + info.name + "\r\n";
                classString += "\t\t{\r\n";
                classString += "\t\t\t get{\r\n";
                classString += "\t\t\t\t   return " + " m_" + info.name + "; \r\n";
                classString += "\t\t\t    }\r\n";
                classString += "\t\t}\r\n";
            }
            classString += "\t public override bool CompareKey(string key)\r\n\t { \r\n";
            List<string> keyNames = new List<string>(data.fieldInfos.Keys);

            classString += "\t\t  return " + keyNames[0] + ".Equals(key); \r\n";

            classString += "\t  } \r\t";
            classString += "}";

            //Debug.Log("classString :" + classString);
            return classString;
        }
    }
}
